package com.linkage.jz.bankruptcy.adapter;

import android.support.v7.widget.RecyclerView;

import com.linkage.jz.bankruptcy.R;
import com.linkage.jz.bankruptcy.bean.NormalModel;
import com.linkage.jz.bankruptcy.bean.NoticeInfo;

import java.util.ArrayList;
import java.util.List;

import cn.bingoogolapple.androidcommon.adapter.BGARecyclerViewAdapter;
import cn.bingoogolapple.androidcommon.adapter.BGAViewHolderHelper;
import cn.bingoogolapple.swipeitemlayout.BGASwipeItemLayout;


public class NoticeRecyclerViewAdapter extends BGARecyclerViewAdapter<NoticeInfo> {
    /**
     * 当前处于打开状态的item
     */
    private List<BGASwipeItemLayout> mOpenedSil = new ArrayList<>();

    public NoticeRecyclerViewAdapter(RecyclerView recyclerView) {
        super(recyclerView, R.layout.item_bgaswipe_notice);
//        super(recyclerView, R.layout.item_bgaswipe);
    }

    @Override
    public void setItemChildListener(BGAViewHolderHelper viewHolderHelper, int viewType) {
        BGASwipeItemLayout swipeItemLayout = viewHolderHelper.getView(R.id.sil_item_bgaswipe_root);
        swipeItemLayout.setDelegate(new BGASwipeItemLayout.BGASwipeItemLayoutDelegate() {
            @Override
            public void onBGASwipeItemLayoutOpened(BGASwipeItemLayout swipeItemLayout) {
                closeOpenedSwipeItemLayoutWithAnim();
                mOpenedSil.add(swipeItemLayout);
            }

            @Override
            public void onBGASwipeItemLayoutClosed(BGASwipeItemLayout swipeItemLayout) {
                mOpenedSil.remove(swipeItemLayout);
            }

            @Override
            public void onBGASwipeItemLayoutStartOpen(BGASwipeItemLayout swipeItemLayout) {
                closeOpenedSwipeItemLayoutWithAnim();
            }
        });
        viewHolderHelper.setItemChildClickListener(R.id.tv_item_bgaswipe_delete);
        viewHolderHelper.setItemChildLongClickListener(R.id.tv_item_bgaswipe_delete);
        viewHolderHelper.setItemChildClickListener(R.id.et_item_bgaswipe_read);
        viewHolderHelper.setItemChildLongClickListener(R.id.et_item_bgaswipe_read);
    }

    @Override
    public void fillData(BGAViewHolderHelper viewHolderHelper, int position, NoticeInfo model) {
        viewHolderHelper
                .setText(R.id.tv_item_bgaswipe_time, model.getOpAt())
                .setText(R.id.tv_item_bgaswipe_detail, model.getTitle())
               ;

        BGASwipeItemLayout swipeItemLayout = viewHolderHelper.getView(R.id.sil_item_bgaswipe_root);
//        if (position % 3 == 0) {
            swipeItemLayout.setSwipeAble(true);
//        } else {
//            swipeItemLayout.setSwipeAble(true);
//        }
    }

    public void closeOpenedSwipeItemLayoutWithAnim() {
        for (BGASwipeItemLayout sil : mOpenedSil) {
            sil.closeWithAnim();
        }
        mOpenedSil.clear();
    }
}